<template>
  <div id="app-container" style="margin: 10px">

    <!--搜索input框 搜索按钮 重置按钮-->
    <el-form :inline="true" style="margin-bottom: -10px">
      <el-form-item label="用户名称:">
        <el-input v-model="queryUserDTO.name" size="small" placeholder="请输入用户名称"/>
      </el-form-item>

      <el-form-item label="VIP类型" prop="vip" clearable>
        <el-select v-model="queryUserDTO.vip" size="small" clearable placeholder="请选择">
          <el-option :value="0" label="非vip"/>
          <el-option :value="1" label="vip"/>
        </el-select>
      </el-form-item>

      <el-form-item label="添加时间:">
        <el-date-picker
          v-model="queryUserDTO.createTime"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          size="small"/>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryUserDTO.updateTime"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          size="small"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="userPageListQuery()">搜索</el-button>
        <el-button plain icon="el-icon-refresh" size="small" style="margin-left: 0" @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>

    <!--新增、修改、删除、导出按钮-->
    <el-form :inline="true">
      <router-link to="/user/add">
        <el-button type="primary" plain icon="el-icon-plus" size="small">新增用户</el-button>
      </router-link>
      <el-button type="danger" plain icon="el-icon-delete" size="small" @click="deleteBySelect">批量删除</el-button>
      <el-button type="warning" plain icon="el-icon-download" size="small" style="margin-left: 0" @click="handleExport">导出数据</el-button>
    </el-form>

    <!--table表格-->
    <el-table
      ref="multipleTable"
      :data="UserList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"/>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (currentPage-1)*pageSize + scope.$index +1 }}
        </template>
      </el-table-column>

      <el-table-column
        prop="name"
        label="用户名称"
        width="150"
        align="center"/>
      <el-table-column
        prop="avatar"
        label="头像"
        width="200"
        align="center">
        <template slot-scope="scope" >
          <img :src="scope.row.avatar" width="45px" height="45px">
        </template>
      </el-table-column>

      <el-table-column label="VIP类型" prop="vip" width="150" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.vip == '0'">非VIP</el-tag>
          <el-tag v-if="scope.row.vip == '1'" type="danger">VIP</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" prop="userType" width="250" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.userType"
            style="display: block;height: 23px;font-weight: 900;font-size: 16px"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="禁止"
            inactive-text="启用"
            :active-value="1"
            :inactive-value="0"
            @change="changeSwitch(scope.row.id,scope.row.userType)"
          />
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        prop="createTime"
        label="创建时间"
        width="200"
        show-overflow-tooltip/>
      <el-table-column
        align="center"
        prop="updateTime"
        label="更新时间"
        width="200"
        show-overflow-tooltip/>

      <el-table-column align="center" label="操作">

        <template slot-scope="scope">
          <router-link :to="'/user/update/'+scope.row.id">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="mini"
            >编辑
            </el-button>
          </router-link>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>
<script>
import user from '../../api/user'
import fileDownload from 'js-file-download'
import axios from 'axios'
export default {
  data() {
    return {
      queryUserDTO: {
      },
      UserList: [],
      currentPage: 1,
      pageSize: 10,
      total: 1,
      ids: [],
      multipleSelection: ['']
    }
  },
  created() {
    this.initDate()
  },
  methods: {
    userPageListQuery() {
      this.initDate()
    },
    resetData() {
      this.queryUserDTO = {}
      this.initDate()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.initDate()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initDate()
    },
    handleDelete(id) {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        this.ids.push(id)
        user.deleteUser(this.ids).then(response => {
          this.$message.success('删除成功')
          this.initDate()
          this.ids = []
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = []
      for (let i = 0; i < val.length; i++) {
        if (this.multipleSelection.indexOf(val[i].id) === -1) {
          this.multipleSelection.push(val[i].id)
        }
      }
      console.log(this.multipleSelection)
    },
    deleteBySelect() {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        user.deleteUser(this.multipleSelection).then(response => {
          this.$message.success('删除成功')
          this.multipleSelection = []
          this.initDate()
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    initDate() {
      user.listPage(this.currentPage, this.pageSize, this.queryUserDTO).then(response => {
        this.UserList = response.data.data
        this.total = response.data.total
      })
    },
    handleExport() {
      console.log('===导出数据===')
      axios({
        method: 'post',
        url: 'http://localhost:8080/user/export',
        responseType: 'blob',
        data: this.queryUserDTO
      }).then(res => {
        if (res && res.data) {
          fileDownload(res.data, '用户.xlsx')
        }
      })
    },

    changeSwitch(id, userType) {
      user.updateType(id, userType).then(resp => {
        if (resp.data.flag) {
          this.$message.success(resp.data.message)
        } else {
          this.$message.error(resp.data.message)
        }
      })
    }
  }
}
</script>
